$(function () {
    var client_datagrid = $("#client_datagrid");
    var client_dialog = $("#client_dialog");
    var client_form = $("#client_form");
    var qo = $("#qo").val();
    var state;
    var clientDevelop_dialog = $("#clientDevelop_dialog");
    var clientDevelopForm = $("#clientDevelopForm");
    var transferClient_dialog = $("#transferClient_dialog");
    var transferClientForm = $("#transferClientForm");
    var jobId = $("#jobId");
    var salaryLevelId = $("#salaryLevelId");
    var clientSourceId = $("#clientSourceId");


    //paris---begin
    //列名变化
    function getClientStateName(qo) {
        console.log(qo);
        if (qo == 2) {
            return "正式客户";
        } else {
            return "开发标识";
        }
    }
    var clientStateName=getClientStateName(qo);
    //paris---end


    // client_datagrid客户列表初始化
    client_datagrid.datagrid({
        fit: true,
        url: "/client/list?state=" + qo,
        fitColumns: true,
        striped: true,
        pagination: true,
        singleSelect: true,
        rownumbers: true,
        pageSize: 10,
        pageList: [3, 5, 10, 15],
        toolbar: "#client_tb",
        sortName:"inputTime",
        sortOrder:"desc",
        columns: [[
            {field: 'ck', checkbox: true},
            {title: '编号', field: 'id', hidden: true},
            {title: '创建日期', field: 'inputTime', width: 100, sortable: true, order: "desc"},
            {
                title: '创建人', field: 'inputUser', width: 100, formatter: function (value) {
                    return value ? value.realname : "未知创建人";
                }
            },
            {
                title: '负责人', field: 'customerManager', width: 100, formatter: function (value) {
                    return value ? value.realname : "未知负责人";
                }
            },
            {title: '客户姓名', field: 'name', width: 100},
            {title: '年龄', field: 'age', width: 100},
            {
                title: '性别', field: 'gender', width: 100, formatter: function (value) {
                    return value ? "男" : "女"
                }
            },
            {title: '电话号码', field: 'phone', width: 100},
            {title: '邮箱', field: 'email', width: 100},
            {title: '地址', field: 'address', width: 100},
            {title: '身份证', field: 'identityCard', width: 100},
            {
                title: '职业', field: 'job', width: 100, formatter: function (value) {
                return value ? value.name : "未知职业";
            }
            },
            {
                title: '收入水平', field: 'salaryLevel', width: 100, formatter: function (value) {
                return value ? value.name : "收入未知";
            }
            },
            {
                title: '客户来源', field: 'clientSource', width: 100, formatter: function (value) {
                return value ? value.name : "客户来源未知";
            }
            },{
                title: clientStateName, field: 'state', width: 100,
                formatter: function (value) {
                    if (value == 0) {
                        return "<font color='green'>可开发客户</font>";
                    } else if (value == 1) {
                        return "<font color='red'>资源池客户</font>";
                    } else if (value == 2) {
                        return "<font color='blue'>正式客户</font>";
                    } else if (value == 3) {
                        return "<font color='#8b0000'>建议转入资源池客户</font>";
                    }
                }
            }
        ]],
        onLoadSuccess: function (data) {

        },
        //新增
        border:false,
        cls:"table table-very table-basic"
    });
    // client_datagrid:END

    // client_dialog:添加编辑对话框初始化
    client_dialog.dialog({
        width: '350px',
        height: '600px',
        buttons: "#dialog_btn",
        closed: true
    });
    // client_dialog:END

    //响应函数集合对象
    var cmdObj = {
        // 客户添加
        add: function () {
            client_dialog.dialog("open");
            client_dialog.dialog("setTitle", "潜在客户添加");
            client_form.form("clear");
        },
        // 客户编辑
        edit: function () {
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择要编辑的行");
                return;
            }
            //清除以前的数据
            client_form.form("clear");
            client_form.form("load", row);
            client_dialog.dialog("open");
            client_dialog.dialog("setTitle", "修改客户信息");

            //rui:客户数据字典编辑回显
            $.get("/dataDictionary/getDataDictionaryById?id=" + row.job.id,
                function (data) {
                    jobId.combobox("setValue", data.id);
                });
            $.get("/dataDictionary/getDataDictionaryById?id=" + row.salaryLevel.id,
                function (data) {
                    salaryLevelId.combobox("setValue", data.id);
                });
            $.get("/dataDictionary/getDataDictionaryById?id=" + row.clientSource.id,
                function (data) {
                    clientSourceId.combobox("setValue", data.id);
                })
            //rui:客户数据字典编辑回显:END

        },
        // rui:修改状态:
        changeClientState: function () {
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择要操作的行");
                return;
            }
            $.messager.confirm('确认', '您确认想要执行该操作吗？', function (r) {
                if (r) {
                    $.get("/client/changeClientState?state=" + state, {id: row.id}, function (data) {
                        if (data.succeed) {
                            $.messager.alert("温馨提示", "操作成功");
                            client_datagrid.datagrid("load");
                        } else {
                            $.messager.alert("温馨提示", data.errorMsg);
                        }
                    })
                }
            });
        },
        // rui:修改状态:END
        // 刷新页面
        reload: function () {
            client_datagrid.datagrid("load");
        },
        // 客户高级查询
        query: function () {
            var keyword = $("#keyword").textbox("getValue");
            var searchManagerRealname = $("#searchManagerRealname").textbox("getValue");
            var beginDate = $("#beginDate").textbox("getValue");
            var endDate = $("#endDate").textbox("getValue");
            client_datagrid.datagrid("load", {
                keyword: keyword,
                searchManagerRealname: searchManagerRealname,
                beginDate: beginDate,
                endDate: endDate
            });
        },
        // 对话框保存信息
        save: function () {
            var id = $("#clientId").val();
            var url = "/client/save";
            if (id) {
                url = "/client/update";
            }
            client_form.form("submit", {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.succeed) {
                        $.messager.alert("温馨提示", "保存成功");
                        client_dialog.dialog("close");
                        client_datagrid.datagrid("load");
                    } else {
                        $.messager.alert("温馨提示", data.errorMsg);
                    }
                }
            })
        },
        // 对话框关闭
        cancel: function () {
            client_dialog.dialog("close");
        },












        //paris代码块-----begin
        //移交客户按钮
        clientTransfer:function(){
            var row = client_datagrid.datagrid("getSelected");
            console.log(row);
            if (!row) {
                $.messager.alert("温馨提示", "请选择要移交的客户");
                return;
            }
            //清除以前的数据
            transferClientForm.form("clear");
            row["oldOustomerManager"]=row.customerManager.realname;
            transferClientForm.form("load", row);
            transferClient_dialog.dialog("open");
        },
        //移交客户提交按钮
        saveTransferClient:function () {
            //获取需要额外封装的数据

            transferClientForm.form("submit",{
                url: "/clientRecord/save",
                onSubmit:function(param){

                    var clientIdToTrans = $("#clientIdToTrans").val();
                        param["client.id"]=clientIdToTrans,
                        param["status"]=1

                },
                success: function (data) {
                    //转化json
                    console.log(data);
                    var json = $.parseJSON(data);
                    if (json.succeed) {
                        //保存成功
                        //关闭弹窗
                        transferClient_dialog.dialog("close");
                        //刷新数据表格
                        client_datagrid.datagrid("reload");
                    } else {
                        $.messager.alert('警告', json.errorMsg);
                    }
                }



            })
        },
        //移交取消按钮
        cancelTransferClient:function(){
            transferClient_dialog.dialog("close");
        },


        //尝试开发按钮
        tryDevelop:function () {
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择要编辑的客户");
                return;
            }
            //清除以前的数据
            clientDevelopForm.form("clear");
            console.log(row);
            $("#clientDevelop_clientId").val(row.id);
            $("#clientDevelop_stateId").val(row.state);
            clientDevelopForm.form("load", row);
            clientDevelop_dialog.dialog("open");
        },

        //开发计划提交按钮
        saveClientDevelop:function () {
            clientDevelopForm.form("submit",{
                url: "/clientDevelopmentPlan/saveOrUpdate",
                success: function (data) {
                    //转化json
                    console.log(data);
                    var json = $.parseJSON(data);
                    if (json.succeed) {
                        //保存成功
                        //关闭弹窗
                        clientDevelop_dialog.dialog("close");
                        //刷新数据表格
                        client_datagrid.datagrid("reload");
                    } else {
                        $.messager.alert('警告', json.errorMsg);
                    }
                }



            })
        },
        //开发计划取消按钮
        cancelClientDevelop:function(){
            clientDevelop_dialog.dialog("close");
        }



        //paris代码块-----end
    };

    // 调用集合函数
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        state = $(this).data("state");
        console.log(state);
        cmdObj[cmd]();
    })






    //paris---begin



    // clientDevelop_dialog:尝试开发计划对话框初始化
    clientDevelop_dialog.dialog({
        title: '客户开发计划',
        width: '700px',
        height: '600px',
        buttons: "#clientDevelop_editbutton",
        closed: true
    });
    // clientDevelop_dialog:移交客户对话框初始化
    transferClient_dialog.dialog({
        title: '移交客户操作',
        width: '700px',
        height: '500px',
        buttons: "#transferClient_editbutton",
        closed: true
    });
    //paris---end

});




